package com.personnalManagerSystem.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.personnalManagerSystem.model.Interview;
import com.personnalManagerSystem.model.Recruit;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-04-06
 */
@Controller
@RequestMapping("/interview")
public class InterviewController  extends  BaseController {

    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize) {
        baseList(model, option, searchContent, pageNum, pageSize, iInterviewService);
        return "view/interview";
    }

    @RequestMapping("/edit")
    /**
     * @Description 修改逻辑和添加逻辑大致相同
     * @author liqing
     * @data 2022/4/7  21:28
     * @param id
     * @param recruitId
     * @param name
     * @param tel
     * @param interviewTime1
     * @param interviewName
     * @return java.lang.String
     */
    public String edit(@RequestParam(value = "id", required = false) Integer id,
                       @RequestParam(value = "recruitId", required = false) Integer recruitId,
                       @RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "tel", required = false) String tel,
                       @RequestParam(value = "interviewTime", required = false) String interviewTime1,
                       @RequestParam(value = "interviewName", required = false) String interviewName) {
        //如果招聘需求id不存在就刷新本页面
        Recruit recruit = iRecruitService.getById(recruitId);
        if (recruit == null) {
            return "redirect:/interview/list";
        }
        Interview interview = new Interview();
        LocalDateTime interviewTime = LocalDateTime.parse(interviewTime1);
        LocalDateTime now = LocalDateTime.now();
        //如果面试预约的时间小于当前时间也刷新本页面
        int i = interviewTime.compareTo(now);
        System.out.println("i=" + i);
        if (i < 0) {
            return "redirect:/interview/list";
        }
        interview.setId(id);
        interview.setRecruitId(recruitId);
        interview.setName(name);
        interview.setTel(tel);
        interview.setInterviewTime(interviewTime);
        interview.setInterviewName(interviewName);
        iInterviewService.saveOrUpdate(interview);
        return "redirect:/interview/list";
    }

    @RequestMapping("/add")
    /**
     * @Description 添加设置默认的招聘状态是未完成, 设置该条记录创建的时间为当前时间, 且面试时间不能小于当前时间, 否则刷新页面
     * @author liqing
     * @data 2022/4/7  21:26
     * @param recruitId
     * @param name
     * @param tel
     * @param interviewTime1
     * @param interviewName
     * @return java.lang.String
     */
    public String add(@RequestParam(value = "recruitId", required = false) Integer recruitId,
                      @RequestParam(value = "name", required = false) String name,
                      @RequestParam(value = "tel", required = false) String tel,
                      @RequestParam(value = "interviewTime", required = false) String interviewTime1,
                      @RequestParam(value = "interviewName", required = false) String interviewName) {
        //如果招聘需求id不存在就刷新本页面
        Recruit recruit = iRecruitService.getById(recruitId);
        if (recruit == null) {
            return "redirect:/interview/list";
        }
        //在添加时,默认设置该面试档案的时间为当前时间
        Interview interview = new Interview();
        LocalDate createTime = LocalDate.now();
        LocalDateTime interviewTime = LocalDateTime.parse(interviewTime1);
        LocalDateTime now = LocalDateTime.now();
        //如果面试预约的时间小于当前时间也刷新本页面
        int i = interviewTime.compareTo(now);
        System.out.println("i=" + i);
        if (i < 0) {
            return "redirect:/interview/list";
        }
        //set注入
        interview.setCreateTime(createTime);
        interview.setInterviewTime(interviewTime);
        //默认添加面试档案记录时,面试状态为未完成,在更新面试结果中可以修改面试状态
        interview.setState("未完成");
        interview.setResult(" ");
        interview.setRecruitId(recruitId);
        interview.setName(name);
        interview.setTel(tel);
        interview.setInterviewName(interviewName);
        iInterviewService.save(interview);
        return "redirect:/interview/list";
    }

    @RequestMapping("/del")
    public String del(@RequestParam(value = "id", required = true) Integer id) {
        System.out.println("id:" + id);
        iInterviewService.removeById(id);
        return "redirect:/interview/list";
    }

    @RequestMapping("/update")
    /**
     * @Description 一个招聘需求有多个面试者, 所以外键建立在面试档案之中,主要是需要更新多张表的数据,还有需要灵活运用构造器
     * @author liqing
     * @data 2022/4/7  23:36
     * @param id
     * @param recruitId
     * @param result
     * @return java.lang.String
     */
    public String update(@RequestParam(value = "id", required = true) Integer id,
                         @RequestParam(value = "recruitId", required = false) Integer recruitId,
                         @RequestParam(value = "result", required = false) String result) {
        //在更新面试结果时,说明面试已经完成,设置面试状态为已完成
        System.out.println("result:" + result);
        Interview interview = new Interview();
        interview.setState("已完成");
        interview.setResult(result);
        interview.setId(id);
        iInterviewService.saveOrUpdate(interview);
        Recruit recruit = iRecruitService.getById(recruitId);
        Integer number = recruit.getNumber();
        //定义一个count用来记录该岗位类型通过的人数
        int count = 0;
        //查询面试档案列表
        QueryWrapper<Interview> wrapper = new QueryWrapper<>();
        wrapper.eq("recruit_id", recruitId);
        List<Interview> list = iInterviewService.list(wrapper);
        //System.out.println("list:" + list);
        List<String> results = list.stream().map(Interview::getResult).collect(Collectors.toList());
       // System.out.println("results:" + results);

            for (String result1 : results) {
                //如果面试状态为通过
                if ("通过".equals(result1)) {
                    count++;
                   // System.out.println("count============"+count);
                    if (count >= number) {
                        //满足要求人数大于等于招聘需求数据,那么就将招聘状态改为已完成
                     //   System.out.println("count:" + count);
                        recruit.setState("已完成");
                        //更新招聘需求中的数据
                        iRecruitService.updateById(recruit);
                        break;
                    }
                }
            }
            if(count<number){
                //如果满足需求的人数小于了招聘的人数,那么就将招聘需求中的状态改为进行中
                recruit.setState("进行中");
                //更新招聘需求中的数据
                iRecruitService.updateById(recruit);
            }

        return "redirect:/interview/list";

    }

}

